package xyz.xtt.pay.controller;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.pay.api.dto.PayAttach;
import xyz.xtt.pay.client.request.MakeOrderRequest;
import xyz.xtt.pay.dto.pay.GoodsInfo;
import xyz.xtt.pay.entity.TblBusinessOrder;
import xyz.xtt.pay.entity.TblPayOrder;
import xyz.xtt.pay.service.INftOrderService;
import xyz.xtt.pay.service.ITblPayOrderService;
import xyz.xtt.pay.vo.CreateOrderRequest;
import xyz.xtt.pay.vo.PayCenterArgsResponseV3;
import xyz.xtt.pay.vo.PayContinueRequest;
import xyz.xtt.pay.vo.paycenter.PayOrderCancelRequest;
import xyz.xtt.pay.vo.paycenter.PayOrderStatusChangeResponse;

@Tag(name = "资产管理", description = "订单管理")
@RefreshScope
@Slf4j
@RequestMapping("/api/nft/pay")
@RestController
public class InnerNftOrderController {
	@Autowired
	private INftOrderService nftOrderService;

	@Value("${pay.center.version:v2}")
	private String payCenterVersion;
	@Autowired
	private ITblPayOrderService iTblPayOrderService;

	@Operation(summary = "下单")
	@PostMapping("/makeOrder")
	@Deprecated
	public ApiResult<?> createPayOrder(@RequestHeader("uid") String uid, @RequestBody @Valid CreateOrderRequest makeOrder) {
		log.info("createPayOrder uid:{}, makeOrder:{}", uid, JSONUtil.toJsonStr(makeOrder));
		if (payCenterVersion != null && Objects.equals(payCenterVersion.toLowerCase(), "v3")) {
			return ApiResult.ok(nftOrderService.createPayOrderV3(uid, makeOrder));
		}
		GoodsInfo goodsInfo = new GoodsInfo();
		return ApiResult.ok(nftOrderService.createPayOrder(uid, makeOrder, goodsInfo));
	}

	@Operation(summary = "支付功能是否可用")
	@GetMapping("/canPay")
	public ApiResult<Boolean> canOperatePay(@RequestHeader("uid") String uid) {
		return ApiResult.ok(nftOrderService.canOperatePay(uid));
	}

	@Operation(summary = "取消订单")
	@PostMapping("/cancelOrder")
	public ApiResult<PayOrderStatusChangeResponse> cancelOrder(@RequestHeader("uid") String uid, @RequestBody @Valid PayOrderCancelRequest request) {
		return ApiResult.ok(nftOrderService.cancelOrder(uid, request));
	}

	@Operation(summary = "继续支付")
	@PostMapping("/continuePay")
	public ApiResult<?> continuePay(@RequestHeader("uid") String uid, @RequestBody @Valid PayContinueRequest continueRequest) {
		continueRequest.getPayOrderId();
		TblPayOrder payOrder = iTblPayOrderService.getPayOrder(Long.valueOf(continueRequest.getPayOrderId()), Long.valueOf(uid));
		if (payOrder.getBusinessOrderId() != null && payOrder.getBusinessOrderId() > 0){
			return ApiResult.ok(nftOrderService.generatePayParam(uid,payOrder,continueRequest.getBackUrl()));
		}
		if (StringUtils.isBlank(payOrder.getBusinessAttach())) {
			return ApiResult.ok(nftOrderService.continuePay(uid, continueRequest));
		}
		PayAttach payAttach = JSONUtil.toBean(payOrder.getBusinessAttach(), PayAttach.class);
		if (payAttach != null && Objects.equals(payAttach.getPayCenterVersion(), "v3")) {
			return ApiResult.ok(nftOrderService.continuePayV3(uid, continueRequest, payOrder));
		}
		return ApiResult.ok(nftOrderService.continuePay(uid, continueRequest));
	}

	@Operation(summary = "下单并自动触发支付")
	@PostMapping("/makeOrder/v2")
	@Transactional(rollbackFor = Exception.class)
	public ApiResult<?> makeOrder(@RequestHeader("uid") String uid, @RequestBody @Valid MakeOrderRequest makeOrderRequest) {
		log.info("makeOrder uid:{}, makeOrderRequest:{}", uid, JSONUtil.toJsonStr(makeOrderRequest));
		// 生成订单
		TblBusinessOrder businessOrder = nftOrderService.makeBusinessOrder(uid, makeOrderRequest);
		// 根据订单生成支付参数
		PayCenterArgsResponseV3 payCenterArgs = nftOrderService.generatePayParam(uid, businessOrder, makeOrderRequest.getBackUrl());
		return ApiResult.ok(payCenterArgs);
	}
}
